﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "selectmodulepage.h"
#include "selectmethodpage.h"
#include "testcaseform.h"
#include <QWizard>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <QDomDocument>
#include <QMdiSubWindow>
#include <QFileDialog>
#include <QCloseEvent>
#include <QMimeData>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)),
            this, SLOT(updateActions()));
    windowActionGroup = new QActionGroup(this);
    windowActionGroup->setExclusive(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionNew_triggered()
{
    QWizard wizard;
    SelectModulePage* selectModulePage = new SelectModulePage(&wizard);
    int id = wizard.addPage(selectModulePage);
    SelectMethodPage* selectMethodPage = new SelectMethodPage(id, &wizard);
    wizard.addPage(selectMethodPage);
    if (QDialog::Accepted == wizard.exec()) {
        TestCaseForm* form = new TestCaseForm(this);
        if (!form->newFile(selectModulePage->getSelectedModuleName(),
                      selectMethodPage->getSelectedMethods())) {
            QMessageBox::critical(this, windowTitle(), QStringLiteral("新建用例失败！"));
            return;
        }
        addForm(form);
    }
}

void MainWindow::on_actionOpen_triggered()
{
    TestCaseForm* form = TestCaseForm::open(this);
    if (form)
        addForm(form);
}

void MainWindow::on_actionSave_triggered()
{
    if (activeForm())
        activeForm()->save();
}

void MainWindow::on_actionSaveAs_triggered()
{
    if (activeForm())
        activeForm()->saveAs();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    ui->mdiArea->closeAllSubWindows();
    if (!ui->mdiArea->subWindowList().isEmpty()) {
        event->ignore();
    }
    else {
        event->accept();
    }
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasUrls())
        event->acceptProposedAction();
    else
        event->ignore();
}

void MainWindow::dropEvent(QDropEvent *event)
{
    const QMimeData* mimeData = event->mimeData();
    if (mimeData->hasUrls()) {
        QList<QUrl> urlList = mimeData->urls();
        QString fileName = urlList.at(0).toLocalFile();
        if (!fileName.isEmpty()) {
            TestCaseForm* form = TestCaseForm::openFile(fileName);
            if (form)
                addForm(form);
        }
    }
}

TestCaseForm *MainWindow::activeForm()
{
    QMdiSubWindow* subWindow = ui->mdiArea->activeSubWindow();
    if (subWindow)
        return qobject_cast<TestCaseForm*>(subWindow->widget());
    else
        return nullptr;
}

void MainWindow::updateActions()
{
    bool hasForm = (activeForm() != nullptr);
    ui->actionSave->setEnabled(hasForm);
    ui->actionSaveAs->setEnabled(hasForm);
    ui->actionClose->setEnabled(hasForm);
    ui->actionCloseAll->setEnabled(hasForm);

    if (activeForm())
        activeForm()->windowMenuAction()->setChecked(true);
}

void MainWindow::addForm(TestCaseForm* form)
{
    QMdiSubWindow* subWindow = ui->mdiArea->addSubWindow(form);
    ui->menuWindow->addAction(form->windowMenuAction());
    windowActionGroup->addAction(form->windowMenuAction());
    subWindow->show();
}

